﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using C2E2C.Entity;
using C2E2C.Data;

namespace C2E2C.Repository
{
    public class RepositoryBase<TEntity> : IRepository<TEntity>
        where TEntity : EntityBase
    {
        private IDbSet<TEntity> dbset;
        private IDbSetContainer dbsetContainer;
        public RepositoryBase(IDbSetContainer dbsetContainer)
        {
            this.dbsetContainer = dbsetContainer;
            this.dbset = dbsetContainer.DbSet<TEntity>();
        }

        public TEntity GetByID(int id)
        {
            return this.dbset.FirstOrDefault(it => it.Id == id);
        }

        public IEnumerable<TEntity> GetAll()
        {
            return this.dbset.ToList();
        }

        public void Add(TEntity entity)
        {
            this.dbset.Add(entity);
        }

        public void Remove(TEntity entity)
        {
            this.dbset.Remove(entity);
        }

        public TEntity Get(Func<TEntity, bool> where)
        {
            return this.dbset.FirstOrDefault(where);
        }

        public IEnumerable<TEntity> GetMany(Func<TEntity, bool> where)
        {
            return this.dbset.Where(where).ToList();
        }

        protected IDbSet<T> DbSet<T>()
            where T : EntityBase
        {
            return this.dbsetContainer.DbSet<T>();
        }
    }
}
